package com.sllp.C71S3TsllpShop.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.SmallType;
import com.sllp.C71S3TsllpShop.biz.BigTypeBiz;
import com.sllp.C71S3TsllpShop.dao.BigTypeMapper;

@Service
public class BigTypeBizImpl implements BigTypeBiz {

	@Resource
	private BigTypeMapper btm;
	@Resource
	private SmallTypeBizImpl stbi;
	
	/**
	 * 主页面全部商品分类显示
	 * 
	 * 将一组数据固定分组，每组n个元素
	 * @param source 要分组的数据源
	 * @param n      每组n个元素
	 * @param
	 * @return
	 */
	public  List<List<BigType>> findAllBigType(List<BigType> source) {
		int n=3;
	    if (null == source || source.size() == 0 || n <= 0)
	        return null;
	    List<List<BigType>> result = new ArrayList<List<BigType>>();
	    int sourceSize = source.size();
	    int size = (source.size() / n) + 1;
	    for (int i = 0; i < size; i++) {
	        List<BigType> subset = new ArrayList<BigType>();
	        for (int j = i * n; j < (i + 1) * n; j++) {
	            if (j < sourceSize) {
	                subset.add(source.get(j));
	            }
	        }
	        result.add(subset);
	    }
	    return result;
	}
	
	
	public List<BigType> findAll(){
		return btm.selectByExample(null);
	}
	
	public Result updateBigTypeState(BigType bigType) {
		SmallType smallType = new SmallType();
		if(bigType.getState()==2) {
			bigType.setState(1);
			smallType.setState(1);
		}else {
			bigType.setState(2);
			smallType.setState(2);
		}
		int code = btm.updateByPrimaryKeySelective(bigType);
		smallType.setParent(bigType.getId());
		code = stbi.updateSmallType(smallType);
		if(code > 0) {
			return new Result(1, "成功");
		}
		return new Result(0, "失败");
	}
	
	public Result addBigType(List<String> bigTypeList) {
		BigType bigType = null;
		int code = 0;
		for(String bigTypeName : bigTypeList) {
			bigType = new BigType(null, bigTypeName, 1);
			code = btm.insertSelective(bigType);
			if(code <=0) {
				return new Result(0, "添加失败");
			}
		}
		return new Result(1, "添加成功");
	}
}


	
	